package com.demo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 使用ScheduleExecutorsService类
 *
 * @author wangning
 * @date 2023/8/3 16:15
 * 特点：使用线程池技术，可实现线程复用
 */
public class ScheduledExecutorServiceDemo {
    //定义时间格式
    private static final DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        service.scheduleAtFixedRate(() -> {
            LocalDateTime now = LocalDateTime.now();
            System.out.println("schedule这是定时任务，时间是：" + pattern.format(now));
        }, 0, 1000, TimeUnit.MILLISECONDS);
    }
}
